/***
 * 零件清单JS文件
 */

		$(document).ready(function(){
			
			GPSbtnControl();//左导航条点击样式控制
		
			listCheck();//选择行
			
			listEdit();//编辑选中的行
			listDel();//删除删除选中的行
		});
		
		
		function GPSbtnControl(){
			$(".pl-GPS a").on("click",function(){
				var thisItem=$(this);
				if(!(thisItem.hasClass("GPSactive"))){
					thisItem.addClass("GPSactive");
					thisItem.siblings().removeClass("GPSactive");
				}
			});
		}
	
		
		function listCheck(){
			$(".list tr td:last-child").on("click",function(){
				var thisItem=$(this);
				
				var thisBox=thisItem.parents(".co-contentList");
				var trSelected=parseInt(thisBox.find(".trSelected").val());
				
				
				var itemParent=thisItem.parent();
				
				if(!(itemParent.hasClass("nowCheck"))){
					itemParent.addClass("nowCheck");
					thisItem.find("i").attr("class","fa fa-check-circle");
					
					trSelected+=1;
					thisBox.find(".trSelected").val(trSelected);
					
				}else{
					itemParent.removeClass("nowCheck");
					thisItem.find("i").attr("class","fa fa-circle-o");
					
					trSelected-=1;
					thisBox.find(".trSelected").val(trSelected);
				}
			});
		}
		
		var partListDesignArray = new Array();
		
		function listEdit(){
			
			$(".co-listEdit").on("click",function(){
				var thisItem=$(this);
				var thisBox=thisItem.parents(".list");
				var trSelected=parseInt(thisBox.find(".trSelected").val());
				var listTrEdit=thisBox.find("tr.nowCheck");
				
 
				if(trSelected == 0){
					alert("请先选中行，再进行编辑操作。");
				}else{
					listTrEdit.find("input").attr("disabled",false);
				}
			});
		}
		
		function saveEditPart(type){
			
			var listTrEdit=$("#container").find("tr.nowCheck");
			$.each(listTrEdit, function(n,value){
				var partsno=$(value).find("input#partsno").val();
				var partsid=$(value).find("input#partsid").val();
				var partsname=$(value).find("input#partsname").val();
				var material=$(value).find("input#material").val();
				var num=$(value).find("input#num").val();
				var price=$(value).find("input#price").val()*100;
				var provider=$(value).find("input#provider").val();
				var comment=$(value).find("input#comment").val();
			
				partListDesignArray.push({id: partsid, partsname: partsname, 
					                      partsno: partsno,material:material,
					                      num:num,price:price,provider:provider,comment:comment});
	        
			});
			
			
			  if(partListDesignArray.length==0 && type==0){
					jAlert("请至少选择一条记录","提示");
				  return false;
			  }else if(partListDesignArray.length==0 && type==1){
				  window.location.href=CONTEXT_PATH+'front/carDesign/carDesign_list';
				  return false;
			  }
			  
	    	  jConfirm('确定要保存吗?', '操作确认', function(r) {
		    	  
		    	     if(r){
		    	           $.ajax({
		    	        	    url:   CONTEXT_PATH+'front/carDesign/saveEditPart', 
		    	                type:  "POST",
		    	                contentType : 'application/json;charset=utf-8', //设置请求头信息
		    	                dataType:"json",
		    	                data:  JSON.stringify(partListDesignArray),            
		    	                success: function(data){
		    	                    if(data.result){
		    	                    	jAlert(data.msg,'提示', function(r) {
			    	                    	if(type==1){
			    	                    			window.location.href=CONTEXT_PATH+'front/carDesign/carDesign_list';
			    	                    	}else{
			    	                    		window.location.reload();
			    	                    	}
		    	                    	});
		    	                    	
					                }else{
					                	jAlert(data.msg,'提示');
					                }
		    	                    
		    	                } 
		    	          });
		    	    }
	
			  });
		}
		
		function exportPDF(designid){
			
			window.location.href=CONTEXT_PATH+'front/carDesign/exportPDF?type=1&designid='+designid;
		}
		
		function listDel(){
			
			
			$(".co-listDel").on("click",function(){
				var idArr=new Array();
				var thisItem=$(this);
				var thisBox=thisItem.parents(".list");
				var trSelected=parseInt(thisBox.find(".trSelected").val());
				var listTrDel=thisBox.find("tr.nowCheck");
				$.each(listTrDel, function(n,value){
					var delid=$(value).find("input#partsid").val();
					idArr.push(delid);
		        });
				 
 
				
	 		    if(idArr.length == 0){
					alert("请先选中行,再进行删除操作。");
				}else{
				 
			    	  
			    	  jConfirm('确定要删除吗?', '操作确认', function(r) {
			    	  
				    	     if(r){
				    	     	  $.ajax({
								     type: 'post',
								     url:   CONTEXT_PATH+'front/carDesign/delpart', 
									 data:  {"ids":idArr.join(",")},
									 dataType: 'json',
									 success: function(data) {  
						                if(data.result){
						                	window.location.reload();
						                }else{
						                	jAlert(data.msg,'提示');
						                }
						             }
									
								 });
				    	     }
			
					  });
				} 
			});
			

			
		}
		
		
		function listAdd(designid,carid,partstype){
			
			$("#partAddModal").modal('show');
			
			$("#partAddModal #designid").val(designid);
			$("#partAddModal #carid").val(carid);
			$("#partAddModal #partstype").val(partstype);
			
		}
		
		
		function submitAddData(){
			
			$("#price").val($("#deci_price").val()*100);
			$("#partform").submit();
		}
		
		
		
		